<template>
  <view class="search">
    <view class="search__place" :style="{height: (44+$u.sys().statusBarHeight)+'px'}"></view>
    <view class="search__fixed">
      <u-status-bar/>
      <view class="search__content" :style="{width: `calc(100% - ${menuBarWidth})`}">
        <text class="iconfont icon-back fz-42 white" v-if="showBack" @click="goBack"/>
        <view class="search-box" @click="$emit('click')">
          <text class="iconfont icon-search"/>
          <input type="text"
                 v-if="!readonly"
                 v-model="keyword"
                 :placeholder="placeholder"
                 placeholder-class="place"
                 @focus="$emit('focus')"
                 @input="$emit('input', $event.target.value)"
                 @confirm="$emit('confirm', $event.target.value)"
          />
          <span class="place" v-else>{{placeholder}}</span>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "search",
  props: {
    value: {
      type: String,
      default: ""
    },
    placeholder: {
      type: String,
      default: "搜索"
    },
    showBack: {
      type: Boolean,
      default: true
    },
    backPage: {
      type: String|Boolean,
      default: ""
    },
    readonly: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {}
  },
  computed: {
    keyword:{
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      }
    }
  },
  methods: {
    goBack() {
      if (typeof this.backPage === "boolean") {
        this.$emit("back");
      } else if (this.backPage) {
        uni.navigateTo({
          url: this.backPage
        })
      } else {
        uni.navigateBack()
      }
    },
  },
  created() {

  },
  watch: {}
}
</script>

<style scoped lang="scss">
.search {
  &__fixed {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #000;
  }

  &__content {
    height: 44px;
    padding: 4px 0 0 32rpx;
    display: flex;
    box-sizing: border-box;
    width: 100%;
    .iconfont {
      line-height: 68rpx;
      margin-right: 20rpx;
    }
    .search-box {
      flex: 1;
      background: #fff;
      border-radius: 44px;
      display: flex;
      align-items: center;
      height: 64rpx;
      padding: 0 20rpx;

      .iconfont {
        font-size: 32rpx;
        color: $-color-muted;
        margin-right: 10rpx;
      }
    }
  }
}
</style>